package com.allwees.bs.c.module.user.controller;

import com.allwees.bs.c.module.base.controller.BaseController;
import com.allwees.bs.c.module.ops.service.CashActiveDefUseService;
import com.allwees.bs.c.module.ops.service.CouponService;
import com.allwees.bs.c.module.ops.service.UserCouponService;
import com.allwees.bs.c.module.user.constant.ECashQueryType;
import com.allwees.bs.c.module.user.constant.EPointQueryType;
import com.allwees.bs.c.module.user.constant.EUserCouponQueryType;
import com.allwees.bs.c.module.user.service.UserCashService;
import com.allwees.bs.c.module.user.service.UserPointService;
import com.allwees.bs.c.module.user.service.UserSignInLogService;
import com.allwees.bs.core.modelbase.page.PageParams;
import com.allwees.bs.core.modelbase.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账户相关
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/user")
public class UserAccountController extends BaseController {

    @Autowired
    private UserCouponService userCouponService;

    @Autowired
    private UserSignInLogService userSignInLogService;

    @Autowired
    private UserPointService userPointService;

    @Autowired
    private UserCashService userCashService;

    @Autowired
    private CashActiveDefUseService cashActiveDefUseService;

    @Autowired
    private CouponService couponService;

    /**
     * 签到
     */
    @PostMapping("/sign-in")
    public R signIn() {
        return R.ok(userSignInLogService.signIn(super.getUserUuid()));
    }

    /**
     * 积分兑换优惠券
     */
    @PostMapping("/{couponDefUuid}/point-exchange-coupon")
    public R pointExchangeCoupon(@PathVariable String couponDefUuid) {
        userPointService.pointExchangeCoupon(super.getUserUuid(), couponDefUuid);
        return R.ok();
    }

    /**
     * 积分 type 查询
     */
    @GetMapping("/point/{queryType}")
    public R pointExchangeCoupon(@PathVariable EPointQueryType queryType, PageParams pageParams) {
        return R.ok(userPointService.page(super.getUserUuid(), queryType, pageParams));
    }

    /**
     * 现金 type 查询
     */
    @GetMapping("/cash/{queryType}")
    public R pointExchangeCoupon(@PathVariable ECashQueryType queryType, PageParams pageParams) {
        return R.ok(userCashService.page(super.getUserUuid(), queryType, pageParams));
    }

    /**
     * 优惠券兑换列表
     */
    @GetMapping("/coupon/point-exchange-list")
    public R findExchangeCoupons() {
        return R.ok(couponService.findExchangeCoupons(super.getUserUuid()));
    }

    /**
     * 优惠券兑换列表
     */
    @GetMapping("/account/static-variable")
    public R staticVariable() {
        return R.ok(cashActiveDefUseService.staticVariable());
    }

    /**
     * 获取有效的优惠券信息
     *
     * @return
     */
    @GetMapping("/coupons/can-use")
    public R couponsCanUse() {
        return R.ok(userCouponService.getCanUseCoupons(getUserUuid()));
    }

    /**
     * 获取有效的优惠券信息
     *
     * @return
     */
    @GetMapping("/coupons/{queryType}")
    public R getCoupons(@PathVariable EUserCouponQueryType queryType, PageParams pageParams) {
        return R.ok(userCouponService.page(getUserUuid(), queryType, pageParams));
    }

    /**
     * 获取有效的优惠券信息
     *
     * @return
     */
    @Deprecated
    @GetMapping("/coupon")
    public R getCanUseCoupons() {
        return R.ok(userCouponService.getCanUseCoupons(getUserUuid()));
    }

    /**
     * 领取优惠券
     *
     * @return
     */
    @PostMapping("/coupon/receive/{couponUuid}")
    public R receiveCoupon(@PathVariable String couponUuid) {
        userCouponService.receive(getUserUuid(), couponUuid);
        return R.ok();
    }
}
